package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.utils.MenuPicUtil;

import java.util.ArrayList;
import java.util.List;


public class EmnuListtemAdapter extends BaseAdapter {

    private static final String TAG = "EmnuListtemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<CookingMenu> items;

    public EmnuListtemAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
    }

    public List<CookingMenu> getData() {
        return items;
    }


    public void setItems(List<CookingMenu> data) {
        items.clear();
        if (data==null||data.isEmpty()) {
            Toast.makeText(context,"该分类没有菜谱",Toast.LENGTH_SHORT).show();
            notifyDataSetChanged();
            return;
        }
        items.addAll(data);
        notifyDataSetChanged();
    }



    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public CookingMenu getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_menu_list_item, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_ct = convertView.findViewById(R.id.tv_ct);
            holder.iv_icon = convertView.findViewById(R.id.iv_icon);
            holder.tv_gg = convertView.findViewById(R.id.tv_gg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String name=items.get(position).getName();
        holder.tv_name.setText(name);
        holder.tv_ct.setText(getCookingType(items.get(position).getCookingType()));
        String pic=items.get(position).getPic();
        holder.tv_gg.setText("规格:"+items.get(position).getSpecification()+"斤");
        if(TextUtils.isEmpty(pic)){
            Glide.with(context).load(R.drawable.menu_icon).into(holder.iv_icon);
        }else {
            Log.e(TAG,"pic="+pic);
            Glide.with(context).load(MenuPicUtil.getInstance(context).getLoaclpicPath(pic,items.get(position).getName())).into(holder.iv_icon);
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_name,tv_ct,tv_gg;
        ImageView iv_icon;
    }

    private String getCookingType(int type){
        if(type==1){
            return "烹饪方式:炒";
        }else if(type==2){
            return "烹饪方式:焖";
        }
        return "烹饪方式:蒸、凉";
    }

}